package com.dyypdadmin.config;

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.multipart.MultipartResolver;
import org.springframework.web.multipart.commons.CommonsMultipartResolver;
import org.springframework.web.servlet.config.annotation.ResourceHandlerRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;

@Configuration
public class WebMvcAutoConfiguration implements WebMvcConfigurer {

    @Bean(name = "multipartResolver")
    public MultipartResolver multipartResolver() {
        CommonsMultipartResolver multipartResolver = new CommonsMultipartResolver();
        return multipartResolver;
    }

    /*
    * 重写addResourceHandlers方法
    * */
//    @Value("${file.uploadFolder}")
//    private String uploadFolder;
//
//    @Value("${file.staticPatternPath}")
//    private String staticPatternPath;
//
//    @Override
//    public void addResourceHandlers(ResourceHandlerRegistry registry) {
//        registry.addResourceHandler(staticPatternPath)
//                .addResourceLocations("file:" + uploadFolder + "/");
//    }
    @Override
    public void addResourceHandlers(ResourceHandlerRegistry registry) {
        String localFilePath = "file:D:/files_store/"; // 本地文件存储路径
        String urlPath = "/files_store/"; // 映射到URL的路径

        registry.addResourceHandler(urlPath + "**")
                .addResourceLocations(localFilePath);
    }

}
